#ifndef __QUADTREE_H__
#define __QUADTREE_H__
#include "Infor.h"
#include "Object.h"
#include <vector>

using std::vector;

#define LIM_W_H 5 //Gioi han kt cua node

//Class node
class QuadNode
{
public:
	vector<Object*>listObj;//Khoi toa RECT cho node

	RECT* _rect;//Khoi toa RECT cho node
	QuadNode *LT,*RT,*LB,*RB;//4 Con tro de tro den cac node tiep theo
	QuadNode()
	{
		_rect=NULL;
		LT=RT=LB=RB=NULL;
	}
	QuadNode(int left,int top,int width,int height)
	{
		//Khoi toa RECT cho node
		_rect=new RECT();
		_rect->left=left;
		_rect->top=top	;
		_rect->right=_rect->left+width;
		_rect->bottom=_rect->top+height;
		LT=RT=LB=RB=NULL;
	}
};
//Class tree

class QuadTree
{
public:
	//Kiem tra va cham giua cac RECT
	bool CheckRECTInRECT(RECT r,RECT rect);
	bool CheckPointInRECT(int x,int y, RECT rect);

	//Lay cac doi tuong thuoc nam trong vung Camera
	void GetObjInRec(QuadNode* root,RECT Screen,vector<Object*>&result);

	QuadNode* _pRoot;//Nut goc
	int		  _mapWidth;
	int	_mapHeght;
	int _count;//Dung de cap phat cho ID Obj
	QuadTree(int w,int h);//Dung de cap phat cho ID Obj
	~QuadTree(void);
	void Add(QuadNode *root,Object *Ob);
	void OutPutTree(QuadNode *root);
	//Dung de cap phat cho ID Obj
	void DrawObjects(int leftRect,RECT Screen);
	//Kiem tra cac doi tuong co va cham voi nha ko
	Object* CheckCollision(QuadNode *root,RECT r,int Seq);
	void CheckCollision(QuadNode *root,RECT r,int Seq,vector<Object*>&result);
	//bool IsOnObject(RECT *rect,int Seq);
	//Up date cac doi tuong trong QuadTree
	void UpdatePosition(QuadNode *root);
	void GetAllObj(QuadNode *root,vector<Object*> &result);
	void RemoveAll(QuadNode *root);

};
#endif


















